package edu.sysu.cshare.gui;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.TimerTask;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

import edu.sysu.cshare.control.ClassManager;
import edu.sysu.cshare.control.HomeworkManager;
import edu.sysu.cshare.control.MessageManager;
import edu.sysu.cshare.control.UserManager;
import edu.sysu.cshare.model.User;
import edu.sysu.cshare.util.gui.GUI;

public class App_Main {

	private JFrame frmCshare;
	private User actor;
	private JPanel pnlMain;

	private static final String HOMEWORKPANEL = "HomeworkPanel";
	private static final String MESSAGEPANEL = "MessagePanel";
	private static final String SHAREPANEL = "SharePanel";
	private static final String SETTINGPANEL = "SettingPanel";
	private static final String HELPPANEL = "HelpPanel";

	private HomeworkManager homeworkManager = null;
	private MessageManager messageManager = null;
	private UserManager userManager = null;
	private ClassManager classManager = null;
	private JLabel lblHomeworkStatus;
	private JLabel lblMessageStatus;
	private static Pnl_Homework pnl_Homework;

	public Pnl_Homework getPnl_Homework() {
		return pnl_Homework;
	}

	public Pnl_Message getPnl_Message() {
		return pnl_Message;
	}

	private static Pnl_Message pnl_Message;

	public User getActor() {
		return actor;
	}

	public void setActor(User actor) {
		this.actor = actor;
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					App_Main window = new App_Main();

					window.frmCshare.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		/*Timer timer = new Timer();
		timer.schedule(new UpdateTask(), 1000, 1000);*/

	}

	static class UpdateTask extends TimerTask {

		@Override
		public void run() {
			try {
				System.out.println("1111");
				if (pnl_Homework != null) {
					pnl_Homework.updateHomework();
				}
				if (pnl_Message != null) {
					pnl_Message.updateMessage();
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
			}
		}

	}

	private void quit() {
		// TODO delete following line
		userManager.logoff();
		System.exit(0);
		int result = JOptionPane.showConfirmDialog(null, "确认关闭", "确认关闭",
				JOptionPane.YES_NO_OPTION);
		if (result == JOptionPane.YES_OPTION) {

			System.exit(0);
		} else {

		}
	}

	/**
	 * Create the application.
	 */
	public App_Main() {

		/*
		 * Dlg_Login dialog = new Dlg_Login(this); dialog.toFront();
		 * dialog.setModal(true);
		 * dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		 * dialog.setVisible(true);
		 */
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			GUI.setUIFont(new javax.swing.plaf.FontUIResource("宋体", Font.PLAIN,
					14));
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		frmCshare = new JFrame();
		frmCshare.setForeground(new Color(30, 144, 255));
		frmCshare.setBackground(new Color(30, 144, 255));
		frmCshare.setIconImage(Toolkit.getDefaultToolkit().getImage(
				App_Main.class
						.getResource("/edu/sysu/cshare/image/CShare_32.png")));

		frmCshare.setBounds(100, 100, 803, 603);
		frmCshare.setLocationRelativeTo(null);
		// frmCshare.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmCshare.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frmCshare.addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent e) {
				quit();
				// System.out.println(result);
			}

		});

		Dlg_Login dlg_Login = new Dlg_Login();
		GUI.showModalDialog(dlg_Login);
		this.actor = dlg_Login.getUser();
		System.out.println(actor.getName() + " 登陆成功");

		this.homeworkManager = new HomeworkManager(actor);
		this.messageManager = new MessageManager(actor);
		this.userManager = new UserManager(actor);

		this.classManager = new ClassManager(actor);

		frmCshare.setTitle("CShare 1.0 欢迎回来" + actor.getName());
		frmCshare.getContentPane().setLayout(new BorderLayout(0, 0));

		pnlMain = new JPanel();
		frmCshare.getContentPane().add(pnlMain, BorderLayout.CENTER);
		pnlMain.setLayout(new CardLayout(0, 0));

		pnl_Homework = new Pnl_Homework(homeworkManager);
		pnl_Homework.setForeground(new Color(224, 255, 255));
		pnl_Homework.setBackground(new Color(240, 248, 255));
		pnlMain.add(pnl_Homework, HOMEWORKPANEL);

		pnl_Message = new Pnl_Message(messageManager);
		pnlMain.add(pnl_Message, MESSAGEPANEL);

		Pnl_Setting pnl_Setting = new Pnl_Setting(userManager, classManager);
		pnlMain.add(pnl_Setting, SETTINGPANEL);

		Pnl_Sharing pnl_Sharing = new Pnl_Sharing(userManager);
		pnlMain.add(pnl_Sharing, SHAREPANEL);

		Pnl_Help pnl_Help = new Pnl_Help();
		pnlMain.add(pnl_Help, HELPPANEL);

		JPanel pnlStatus = new JPanel();
		frmCshare.getContentPane().add(pnlStatus, BorderLayout.SOUTH);
		pnlStatus.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		lblHomeworkStatus = new JLabel("我的作业:");

		lblHomeworkStatus.setToolTipText("未交/总数");
		pnlStatus.add(lblHomeworkStatus);

		lblMessageStatus = new JLabel("消息: ");
		lblMessageStatus.setToolTipText("新/总数");
		pnlStatus.add(lblMessageStatus);

		JPanel pnlOption = new JPanel();
		//pnlOption.setBackground(new Color(30, 144, 255));
		//pnlOption.setForeground(new Color(30, 144, 255));
		frmCshare.getContentPane().add(pnlOption, BorderLayout.NORTH);
		pnlOption.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		JButton btnHomework = new JButton("");
		/*btnHomework.setForeground(new Color(138, 43, 226));
		btnHomework.setBackground(new Color(138, 43, 226));*/
		btnHomework.setToolTipText("作业管理");
		btnHomework.setVerticalTextPosition(SwingConstants.BOTTOM);
		btnHomework.setVerticalAlignment(SwingConstants.BOTTOM);

		btnHomework.setHorizontalTextPosition(SwingConstants.CENTER);
		btnHomework.setIcon(new ImageIcon(App_Main.class
				.getResource("/edu/sysu/cshare/image/homework_64.png")));
		btnHomework.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CardLayout cl = (CardLayout) pnlMain.getLayout();
				cl.show(pnlMain, HOMEWORKPANEL);

			}
		});
		pnlOption.add(btnHomework);

		JButton btnMsg = new JButton("");
		/*btnMsg.setForeground(new Color(138, 43, 226));
		btnMsg.setBackground(new Color(138, 43, 226));*/
		btnMsg.setToolTipText("消息管理");
		btnMsg.setIcon(new ImageIcon(App_Main.class
				.getResource("/edu/sysu/cshare/image/comment_edit-64.png")));
		btnMsg.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CardLayout cl = (CardLayout) pnlMain.getLayout();
				cl.show(pnlMain, MESSAGEPANEL);
			}
		});
		pnlOption.add(btnMsg);

		JButton btnSharing = new JButton("");
		/*btnSharing.setForeground(new Color(138, 43, 226));
		btnSharing.setBackground(new Color(138, 43, 226));*/
		btnSharing.setToolTipText("文件共享");
		btnSharing.setVerticalAlignment(SwingConstants.BOTTOM);
		btnSharing.setIcon(new ImageIcon(App_Main.class
				.getResource("/edu/sysu/cshare/image/share2_64.png")));
		btnSharing.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CardLayout cl = (CardLayout) pnlMain.getLayout();
				cl.show(pnlMain, SHAREPANEL);
			}
		});
		pnlOption.add(btnSharing);

		JButton btnSetting = new JButton("");
		/*btnSetting.setForeground(new Color(138, 43, 226));
		btnSetting.setBackground(new Color(138, 43, 226));*/
		btnSetting.setToolTipText("系统设置");
		btnSetting.setIcon(new ImageIcon(App_Main.class
				.getResource("/edu/sysu/cshare/image/setting2_64.png")));
		btnSetting.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CardLayout cl = (CardLayout) pnlMain.getLayout();
				cl.show(pnlMain, SETTINGPANEL);
			}
		});
		pnlOption.add(btnSetting);

		JButton btnHelp = new JButton("");
		/*btnHelp.setForeground(new Color(138, 43, 226));
		btnHelp.setBackground(new Color(138, 43, 226));*/
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CardLayout cl = (CardLayout) pnlMain.getLayout();
				cl.show(pnlMain, HELPPANEL);
			}
		});
		btnHelp.setToolTipText("帮助中心");
		btnHelp.setIcon(new ImageIcon(App_Main.class
				.getResource("/edu/sysu/cshare/image/help2_64.png")));
		pnlOption.add(btnHelp);

		JButton btnQuit = new JButton("");
		/*btnQuit.setForeground(new Color(138, 43, 226));
		btnQuit.setBackground(new Color(138, 43, 226));*/
		btnQuit.setToolTipText("退出系统");
		btnQuit.setIcon(new ImageIcon(App_Main.class
				.getResource("/edu/sysu/cshare/image/quit_64.png")));
		btnQuit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				quit();
				// System.exit(1);
			}
		});
		pnlOption.add(btnQuit);
		updateValue();
	}

	public void updateValue() {
		try {
			lblHomeworkStatus.setText("我的作业:"
					+ homeworkManager.getUnsubmit().size() + "/"
					+ homeworkManager.getAll().size());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
